import Vue from 'vue'
import VueRouter from 'vue-router'
import Router from 'vue-router'
import {getToken} from '@/utils/auth'
import Nprogress from "nprogress";

Vue.use(VueRouter)

//解决vue路由重复导航错误
//获取原型对象上的push函数
const originalPush = Router.prototype.push
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


const routes = [
    {path: '/login', name: 'Login', component: () => import('@/views/login/index'), meta: {title: "登录"}},
    {
        path: '/', component: () => import('@/components/layout/index'), redirect: '/home',
        children: [
            {
                path: 'home',
                name: 'Home',
                component: () => import('@/views/home/index')
            },
            {
                name: "404",
                path: '404',
                component: () => import('@/views/404')
            },
            {
                name: "Role",
                path: 'sys/role',
                component: () => import('@/views/system/role/index')
            },
            {
                name: "User",
                path: 'sys/user',
                component: () => import('@/views/system/user/index')
            },
            {
                name: "Menu",
                path: 'sys/menu',
                component: () => import('@/views/system/menu/index')
            },
            {
                name: "UserDetail",
                path: 'user/detail',
                component: () => import('@/views/userDetail/index')
            },
            {
                name: "Exam",
                path: 'exam',
                component: () => import('@/views/student/exam/index')
            },
            {
                name: "Habit",
                path: 'habit',
                component: () => import('@/views/student/habit/index')
            },
            {
                name: "ExamDetail",
                path: 'exam/detail',
                component: () => import('@/views/student/exam/detail/index')
            },
            {
                name: "StudentAdmin",
                path: 'doctor/student',
                component: () => import('@/views/doctor/student/index')
            },
            {
                name: "ExamAdmin",
                path: 'doctor/exam',
                component: () => import('@/views/doctor/exam/index')
            },
        ]
    }
]

const router = new VueRouter({
    routes
})
const whiteList = ['/login', '/register']

router.beforeEach(async (to, from, next) => {
    // 开启进度条
    Nprogress.start();
    if (to.matched.length === 0) {
        next('/404') // 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
    }
    const token = getToken()
    if (!token && to.name !== 'Login') {
        next({name: 'Login'})
    } else if (token && to.name === 'Login') {
        next({name: 'Home'})
    } else if (!token && whiteList.indexOf(to.path) !== -1) {
        next()
    } else if (token) {
        next()
    }
})
// 页面路由切换完毕的时候
router.afterEach(() => {
    // 关闭进度条
    Nprogress.done()
})

export default router
